package com.qingcloud.adminbackend.controller;

import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.upgrade.EmbeddedBinDTO;
import com.qingcloud.adminbackend.entity.upgrade.EmbededRequest;
import com.qingcloud.adminbackend.service.impl.ITdEmbeddedBinServiceImpl;
import com.qingcloud.adminbackend.utils.CookieUtil;
import com.qingcloud.adminbackend.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(value = "升级bin文件管理", description = "升级bin文件管理")
@RestController
@RequestMapping("/embeddedBinManage")
public class TdEmbeddedBinController {

    @Resource
    private ITdEmbeddedBinServiceImpl embeddedBinService;

    /**
     * 分页查询
     *
     * @param embeddedBin
     * @return
     */
    @ApiOperation(value = "分页查询BIN文件信息", notes = "条件分页查询BIN文件信息", response = EmbeddedBinDTO.class)
    @PostMapping(value = "/getEmbedbedBin")
    public AjaxResult getEmbedbedBin(@RequestBody EmbededRequest embeddedBin) {
        return embeddedBinService.getEmbedbedBin(embeddedBin);
    }

    @ApiOperation(value = "新增BIN文件信息", notes = "新增BIN文件信息")
    @PostMapping(value = "/addEmbeddedBin")
    public AjaxResult addEmbeddedBin(@RequestBody EmbeddedBinDTO embeddedBin, HttpServletRequest request) {
        String userName = CookieUtil.getUserName(request);
        embeddedBin.setCreateBy(userName);
        embeddedBin.setModifyBy(userName);
        return embeddedBinService.addEmbedbedBin(embeddedBin);
    }


    /**
     * 获取指定BIN文件信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取指定BIN文件信息", notes = "获取指定id的BIN文件信息", response = EmbeddedBinDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "BIN文件ID", required = true, dataType = "string", paramType = "path", defaultValue = "1"),
    })
    @GetMapping(value = "/getEmbedbedBinById/{id}")
    public AjaxResult getEmbedbedBinById(@PathVariable("id") String id, HttpServletRequest request) {
        String prefixUrl = RequestUtil.getRequestUrl(request) + "/file/view/";
        EmbeddedBinDTO embedbedBin = embeddedBinService.getEmbedbedBinById(id);
        embedbedBin.setFullUrl(prefixUrl + embedbedBin.getFileUrl());
        return new AjaxResult(WamingConstant.SUCCESS, "", embedbedBin);
    }

    /**
     * 修改BIN文件信息
     *
     * @param embeddedBin
     * @return
     */
    @ApiOperation(value = "修改BIN文件信息", notes = "修改BIN文件信息")
    @PostMapping(value = "/updateEmbedbedBin")
    public AjaxResult updateEmbedbedBin(@RequestBody EmbeddedBinDTO embeddedBin, HttpServletRequest request) {
        String userName = CookieUtil.getUserName(request);
        embeddedBin.setModifyBy(userName);
        return embeddedBinService.updateEmbedbedBin(embeddedBin);
    }

    /**
     * 删除BIN文件信息
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除BIN文件信息", notes = "删除BIN文件信息")
    @PostMapping(value = "/deleteEmbedbedBin")
    public AjaxResult deleteEmbedbedBin(@RequestParam("ids[]") String[] ids) {
        return embeddedBinService.deleteEmbedbedBin(ids);
    }

    @ApiOperation(value = "查询所有BIN文件信息", notes = "查询所有BIN文件信息")
    @GetMapping(value = "/findAllBin")
    public AjaxResult findAllBin() {
        return embeddedBinService.findAllBin();
    }

}
